package com.psmz.mqim.model;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.xml.sax.SAXException;

import com.psmz.mqim.Constants;
import com.psmz.mqim.model.xml.TagNames;
import com.psmz.mqim.model.xml.TextMessageParserHandler;
import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory;

/**
 * Wiadomosc komunikatora
 * 
 * @author pszwarc
 * 
 */
public class IMMessage {
	private IMMessageType type;
	private String fromUser;
	private String toUser;
	private Date sendDate;
	private String payload;

	public IMMessageType getType() {
		return type;
	}

	public void setType(IMMessageType type) {
		this.type = type;
	}

	public String getFromUser() {
		return fromUser;
	}

	public void setFromUser(String fromUser) {
		this.fromUser = fromUser;
	}

	public String getToUser() {
		return toUser;
	}

	public void setToUser(String toUser) {
		this.toUser = toUser;
	}

	public String getPayload() {
		return payload;
	}

	public void setPayload(String payload) {
		this.payload = payload;
	}

	public Date getSendDate() {
		return sendDate;
	}

	public void setSendDate(Date sendDate) {
		this.sendDate = sendDate;
	}

	public String toXml() throws XMLStreamException {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		XMLStreamWriter writer = XMLStreamWriterFactory.create(outputStream);
		try {
			writer.writeStartDocument();
			writer.writeStartElement(TagNames.ROOT_TAG);
			writer.writeStartElement(TagNames.TYPE_TAG);
			writer.writeCharacters(type.toString());
			writer.writeEndElement();
			writer.writeStartElement(TagNames.FROM_TAG);
			writer.writeCharacters(fromUser);
			writer.writeEndElement();
			if (type == IMMessageType.TEXT_MESSAGE) {
				writer.writeStartElement(TagNames.TO_TAG);
				writer.writeCharacters(toUser);
				writer.writeEndElement();
				writer.writeStartElement(TagNames.SEND_DATE_TAG);
				SimpleDateFormat format = new SimpleDateFormat(
						Constants.DATE_FORMAT);
				String formattedDate = format.format(sendDate);
				writer.writeCharacters(formattedDate);
				writer.writeEndElement();
				writer.writeStartElement(TagNames.CONTENT_TAG);
				writer.writeCharacters(payload);
				writer.writeEndElement();
			}
			writer.writeEndElement();
			writer.writeEndDocument();
		} finally {
			writer.close();
		}

		return new String(outputStream.toByteArray());
	}

	public String format() {
		StringBuilder sb = new StringBuilder();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				Constants.DATE_FORMAT);
		String formattedDate = simpleDateFormat.format(sendDate);
		sb.append("\n");
		sb.append("OD: ");
		sb.append(fromUser);
		sb.append(" DO: ");
		sb.append(toUser);
		sb.append("\n");
		sb.append(formattedDate);
		sb.append("\n");
		sb.append(payload);
		return sb.toString();
	}

	public static IMMessage create(String xml)
			throws ParserConfigurationException, SAXException, IOException {
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		SAXParser saxParser = parserFactory.newSAXParser();
		TextMessageParserHandler handler = new TextMessageParserHandler();
		saxParser.parse(new ByteArrayInputStream(xml.getBytes()), handler);
		return handler.getParsedMessage();
	}

}
